﻿using System;
using System.Collections.Generic;
using System.Text;
using Opalis.QuickIntegrationKit;
using System.Management;
using System.Management.Instrumentation;
using OpalisSCCMInterop;
using Microsoft.ConfigurationManagement;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;

namespace OpalisSCCMExtension
{
    [OpalisObject("Modify SCCM Collection: Add Direct Member")]
    public class ModifyCollectionAddDirectMember : IOpalisObject
    {
        private ConnectionCredentials settings;
        private String userName = String.Empty;
        private String password = String.Empty;
        private String SCCMServer = String.Empty;

        private int ObjCount = 0;

        [OpalisConfiguration]
        public ConnectionCredentials Settings
        {
            get { return settings; }
            set { settings = value; }
        }
        public void Design(IOpalisDesigner designer)
        {
            WqlConnectionManager connection = CMInterop.connectSCCMServer(settings.SCCMSERVER, settings.UserName, settings.Password);
            String[] membershipTypeChoices = new String[] { "System","User","User Group" };
            designer.AddInput("Collection ID").WithDefaultValue("ABC00000");
            designer.AddInput("Direct Membership Type").WithDefaultValue("System").WithListBrowser(membershipTypeChoices);
            designer.AddInput("ResourceID").WithDefaultValue(1);
            
            designer.AddCorellatedData(typeof(collection));
            designer.AddOutput("Number of Collections");
        }
        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
            SCCMServer = settings.SCCMSERVER;
            userName = settings.UserName;
            password = settings.Password;

            String objID = request.Inputs["Collection ID"].AsString();
            String membershipType = request.Inputs["Direct Membership Type"].AsString();
            int resourceID = (int)request.Inputs["ResourceID"].AsUInt32();

            //Setup WQL Connection and WMI Management Scope
            WqlConnectionManager connection = CMInterop.connectSCCMServer(SCCMServer, userName, password);

            switch (membershipType)
            {
                case "System":
                    CMInterop.addDirectSCCMCollectionMemberMachine(connection, objID, resourceID);
                    break;
                case "User":
                    CMInterop.addDirectSCCMCollectionMemberUser(connection, objID, resourceID);
                    break;
                case "User Group":
                    CMInterop.addDirectSCCMCollectionMemberUserGroup(connection, objID, resourceID);
                    break;
                default:
                    response.LogErrorMessage("Invalid Direct Membership Type");
                    break;
            }

            IResultObject col = null;
            col = CMInterop.getSCCMCollection(connection, "CollectionID LIKE '" + objID + "'");

            if (col != null)
            {
                response.WithFiltering().PublishRange(getObjects(col));

            }
            response.Publish("Number of Collections", ObjCount);
        }
        private IEnumerable<collection> getObjects(IResultObject objList)
        {
            foreach (IResultObject obj in objList)
            {
                ObjCount++;
                yield return new collection(obj);
            }
        }
    }
}
